//Source file: C:\\uml-generated-code\\com\\club325\\voicemicroblog\\Business\\AdminControllerImplement.java

package com.club325.voicemicroblog.persist;

import com.club325.voicemicroblog.database.hibernateutil.HibernateUtil;
import com.club325.voicemicroblog.domain.ForbiddenUser;
import com.club325.voicemicroblog.domain.ForbiddenIP;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

/**
 * Class AdminControllerImplement
 */
public class AdminControllerImplement implements AdminControllerUtil {
	public ForbiddenUser theForbiddenUser;
	public ForbiddenIP theForbiddenIP;

	/**
	 * Fields Constructors
	 */
	public AdminControllerImplement() {
	}

	/**
	 * @return java.util.ArrayList
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<ForbiddenUser> getForbiddenUserList() {
		try
		{
			Criteria crit = HibernateUtil.getSession().createCriteria(ForbiddenUser.class);
			crit.add(Restrictions.ge("EndTime", new Timestamp(System.currentTimeMillis())));
			return new ArrayList<ForbiddenUser>(crit.list());
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @param forbidInfo
	 * @return java.lang.Boolean
	 */
	public Boolean addForbiddenUser(ForbiddenUser forbidInfo) {
		try
		{
			// 查询当前用户是否被封禁
			Criteria crit = HibernateUtil.getSession().createCriteria(ForbiddenUser.class);
			crit.add(Restrictions.eq("UserID", forbidInfo.getUserID()));
			crit.add(Restrictions.gt("EndTime", new Timestamp(System.currentTimeMillis())));
			List<?> result = crit.list();
			
			// 当List为空的时候就表示目前用户没有被封�?
			if(result.isEmpty())
			{
				HibernateUtil.add(forbidInfo);
				return true;
			}
			else
			{
				return false;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * @param UserID
	 * @return java.lang.Boolean
	 */
	public Boolean releaseForbiddenUser(String UserID) {
		try
		{
			// 查询当前用户是否被封禁
			Criteria crit = HibernateUtil.getSession().createCriteria(ForbiddenUser.class);
			crit.add(Restrictions.eq("UserID", UserID));
			crit.add(Restrictions.gt("EndTime", new Timestamp(System.currentTimeMillis())));
			List<?> result = crit.list();
			
			if(result.isEmpty())
			{
				return false;
			}
			else
			{
				ForbiddenUser obj = (ForbiddenUser) crit.uniqueResult();
				obj.setEndTime(new Timestamp(System.currentTimeMillis()));
				HibernateUtil.Update(obj);
				return true;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * @param forbidInfo
	 * @return java.lang.Boolean
	 */
	public Boolean addForbiddenIP(ForbiddenIP forbidInfo) {
		try
		{
			// 查询当前IP是否被封封禁
			Criteria crit = HibernateUtil.getSession().createCriteria(ForbiddenIP.class);
			crit.add(Restrictions.eq("IP", forbidInfo.getIP()));
			crit.add(Restrictions.gt("EndTime", new Timestamp(System.currentTimeMillis())));
			List<?> result = crit.list();
			if(result.isEmpty())
			{
				HibernateUtil.add(forbidInfo);
				return true;
			}
			else
			{
				return false;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * @param IP
	 * @return java.lang.Boolean
	 */
	public Boolean releaseForbiddenIP(String IP) {
		try
		{
			// 查询当前用户是否被封禁
			Criteria crit = HibernateUtil.getSession().createCriteria(ForbiddenIP.class);
			crit.add(Restrictions.eq("IP", IP));
			crit.add(Restrictions.gt("EndTime", new Timestamp(System.currentTimeMillis())));
			List<?> result = crit.list();
			
			if(result.isEmpty())
			{
				return false;
			}
			else
			{
				ForbiddenIP obj = (ForbiddenIP) crit.uniqueResult();
				obj.setEndTime(new Timestamp(System.currentTimeMillis()));
				HibernateUtil.Update(obj);
				return true;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * @return java.util.ArrayList
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<ForbiddenIP> getForbiddenIPList() {
		try
		{

			Criteria crit = HibernateUtil.getSession().createCriteria(ForbiddenIP.class);
			crit.add(Restrictions.ge("EndTime", new Timestamp(System.currentTimeMillis())));
			return new ArrayList<ForbiddenIP>(crit.list());
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public boolean isUserForbidden(String UserID) {
		try
		{
			Criteria crit = HibernateUtil.getSession().createCriteria(ForbiddenUser.class);
			crit.add(Restrictions.ge("EndTime", new Timestamp(System.currentTimeMillis())));
			crit.add(Restrictions.eq("UserID",UserID));
			return !(crit.list().isEmpty());
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

	public boolean isIPForbidden(String IP) {
		try
		{
			Criteria crit = HibernateUtil.getSession().createCriteria(ForbiddenIP.class);
			crit.add(Restrictions.ge("EndTime", new Timestamp(System.currentTimeMillis())));
			crit.add(Restrictions.eq("IP",IP));
			return !(crit.list().isEmpty());
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}
}
